package com.qianfeng.zookeeper.zookeeper;

//
//                            _ooOoo_  
//                           o8888888o  
//                           88" . "88  
//                           (| -_- |)  
//                            O\ = /O  
//                        ____/`---'\____  
//                      .   ' \\| |// `.  
//                       / \\||| : |||// \  
//                     / _||||| -:- |||||- \  
//                       | | \\\ - /// | |  
//                     | \_| ''\---/'' | |  
//                      \ .-\__ `-` ___/-. /  
//                   ___`. .' /--.--\ `. . __  
//                ."" '< `.___\_<|>_/___.' >'"".  
//               | | : `- \`.;`\ _ /`;.`/ - ` : | |  
//                 \ \ `-. \_ __\ /__ _/ .-` / /  
//         ======`-.____`-.___\_____/___.-`____.-'======  
//                            `=---='  
//  
//         .............................................  
//                  佛祖镇楼                  BUG辟易  
//          佛曰:  
//                  写字楼里写字间，写字间里程序员；  
//                  程序人员写程序，又拿程序换酒钱。  
//                  酒醒只在网上坐，酒醉还来网下眠；  
//                  酒醉酒醒日复日，网上网下年复年。  
//                  但愿老死电脑间，不愿鞠躬老板前；  
//                  奔驰宝马贵者趣，公交自行程序员。  
//                  别人笑我忒疯癫，我笑自己命太贱；  
//  


import com.qianfeng.zookeeper.utils.ConnectionUtil;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;

import java.util.List;
import java.util.concurrent.CountDownLatch;

/**
 * Created by Jackiechan on 2022/6/29 11:38
 *
 * @author Jackiechan
 * @version 1.0
 * @since 1.0
 */
public class RegistryDemo implements Watcher {
    private static ZooKeeper zooKeeper;
    private static CountDownLatch countDownLatch = new CountDownLatch(1);

    @Override
    public void process(WatchedEvent event) {
        System.err.println("ZookeeperWatcher03中的方法process 执行了");
        Event.EventType type = event.getType();
        Event.KeeperState state = event.getState();
        //受影响的路径
        String path = event.getPath();
        if (state == Event.KeeperState.SyncConnected) {
            switch (type) {
                case None:
                    System.err.println("连接成功");
                    break;
                case NodeCreated:
                    System.err.println(path + "创建成功");
                    //当这个节点创建的时候我们就开始监听
                 try {
                     //我们获取了子节点,并且期望监听,肯定是子节点发生变化
                     List<String> children = zooKeeper.getChildren(path, true);
                     children.forEach(System.err::println);
                 }catch (Exception e){
                    e.printStackTrace();
                 }
                    break;
                case NodeDataChanged:
                    System.err.println(path + "数据更新成功");
                    try {
                        byte[] data = zooKeeper.getData(path, true, null);
                        System.err.println("当前节点的新数据:" + new String(data));
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                    break;
                case NodeChildrenChanged:
                    //子节点发生变化,这里返回的是哪个父节点发生了变化
                    System.err.println("子节点发生变化了");
                    try {
                        List<String> children = zooKeeper.getChildren(path, true);
                        children.forEach(System.err::println);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;
            }
        }
        countDownLatch.countDown();
    }


    public static void main(String[] args) throws Exception {
        RegistryDemo zookeeperWatcher03 = new RegistryDemo();
        //创建一个zookeeper
        zooKeeper = new ZooKeeper(ConnectionUtil.SERVER_ADDRESS, 30000, zookeeperWatcher03);
        countDownLatch.await();
        Stat exists = zooKeeper.exists("/05orderconsumer", true);
        if (exists != null) {//如果存在我就监听,不存在就等存在的时候再监听
            //我们获取了子节点,并且期望监听,肯定是子节点发生变化
            List<String> children = zookeeperWatcher03.getChildren("/05orderconsumer", true);
            children.forEach(System.err::println);
        }

        System.in.read();
        System.err.println("程序结束结束了");

    }

    public List<String> getChildren(String parent, boolean watch) throws Exception {
        return zooKeeper.getChildren(parent, watch);
    }

}
